const router = require('koa-router')()

const {query} = require('../db/koa-mysql-prod')

router.get('/', async (ctx, next) => {
  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})

router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})


async function selectAllData( ) {
  console.log("selectAllData method")
  let sql = 'SELECT * FROM blog'
  let dataList = await query( sql )
  return dataList
}

async function save( ) {
  console.log("save method")
  
  let sql = 'INSERT INTO `blog` (`title`, `content`, `category`, `startArticle`, `authorAge`) VALUES (?,?,?,?,?)';
  let params = [`客户端生成二维码nodejs_v3_hotUpdate`, `sssssssssssss学`, `111`, `0`, 8]
  let dataList = await query( sql,params)
  return dataList
}

router.get('/mysql', async (ctx, next) => {
  await save()
  ctx.body = await selectAllData()
})

module.exports = router
